﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace ControleRepresentante.Logic.Models
{
    public class Attribute : ModelBase
    {
        private readonly IList<AttributeValue> m_AttributeValues;
        private readonly IList<AttributeIndexColumnPreference> m_AttributeIndexColumnPreferences;

        public Attribute()
        {
            m_AttributeValues = new List<AttributeValue>();
            m_AttributeIndexColumnPreferences = new List<AttributeIndexColumnPreference>();
        }

        public virtual Representante Representante { get; set; }
        public virtual AttributeType Type { get; set; }
        [Required(ErrorMessage = "*")]
        public virtual string Name { get; set; }
        protected internal virtual IEnumerable<AttributeValue> AttributeValues { get { return m_AttributeValues; } }
        protected internal virtual IEnumerable<AttributeIndexColumnPreference> AttributeIndexColumnPreferences { get { return m_AttributeIndexColumnPreferences; } }

        protected internal virtual void AddAttributeValue(AttributeValue attributeValue)
        {
            m_AttributeValues.Add(attributeValue);
            attributeValue.Attribute = this;
        }

        protected internal virtual void RemoveAttributeValue(AttributeValue attributeValue)
        {
            m_AttributeValues.Remove(attributeValue);
            attributeValue.Attribute = null;
        }

        public virtual void ClearAttributeValues()
        {
            foreach (AttributeValue attributeValue in m_AttributeValues.ToArray())
            {
                attributeValue.AttributeHolder.RemoveAttributeValue(attributeValue);
                RemoveAttributeValue(attributeValue);
            }
        }

        public virtual void AddAttributeIndexColumnPreferences(AttributeIndexColumnPreference attributeIndexColumnPreference)
        {
            m_AttributeIndexColumnPreferences.Add(attributeIndexColumnPreference);
        }

        public virtual void RemoveAttributeIndexColumnPreferences(AttributeIndexColumnPreference attributeIndexColumnPreference)
        {
            m_AttributeIndexColumnPreferences.Remove(attributeIndexColumnPreference);
        }
    }

    public class AttributeValue : ModelBase
    {
        public virtual Attribute Attribute { get; set; }
        public virtual AttributeHolder AttributeHolder { get; protected internal set; }
        public virtual string Value { get; set; }
    }

    public enum AttributeType
    {
        Vendedor,
        [Description("Fábrica")]
        Fabrica,
        Transportadora,
        Cliente,
        Pedido,
        [Description("Relatório de Visita")]
        RelatorioVisita
    }
}